/*
 This is the application bean, 
 */
package view;

import controller.Facade;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;
import model.integration.Application;
import model.integration.CompetenceList;
import model.integration.DateList;



/**
 *
 * @author Ahmed Salem
 */
@Named(value = "applicationBean")
@ConversationScoped

public class Applicationbean implements Serializable {

    private static final long serialVersionUID = 16247164405L;
    @EJB
    private Facade controller; 
   
    
  
    private String firstname;
    private String surname;
    private String ssn;
    private String email;
    private String fromDate;
    private String toDate;
    private String competence;
    private String yearOfExperence;
    private boolean loginStatus = false;
    private List<DateList> dateList;// = new ArrayList <DateList>();
    private List<CompetenceList> comList;// = new ArrayList<CompetenceList>();
    @Inject
    private Conversation conversation;
   
    /**
     *
     */
    public Applicationbean() {
    }
    
    // This method starts the conversation 
    private void startConversation() 
    {
        if (conversation.isTransient()) 
        {
            conversation.begin();
        }
    }

   // This method stops the conversation  
    private void stopConversation() 
    {
        if (!conversation.isTransient()) 
        {
            conversation.end();
        }
    }
   
    /**
     *
     * @param firstname 
     */
    public void setFirstName(String firstname) 
    {this.firstname = firstname;}
    
    /**
     *
     * @param surname
     */
    public void setSurname(String surname) 
    {this.surname = surname;}
    
    /**
     *
     * @param ssn
     */
    public void setSSN(String ssn) 
    {this.ssn = ssn;}
    
    /**
     *
     * @param email
     */
    public void setEmail(String email) 
    {this.email = email;}
    
    /**
     *
     * @param fromDate
     */
    public void setFromDate(String fromDate) 
    {this.fromDate = fromDate;}
    
    /**
     *
     * @param toDate
     */
    public void setToDate(String toDate) 
    {this.toDate = toDate;}
    
    /**
     *
     * @param competence
     */
    public void setCompetence(String competence) 
    {this.competence = competence;}
    
    /**
     *
     * @param yearOfExperence
     */
    public void setYearOfExperence(String yearOfExperence) 
    {this.yearOfExperence = yearOfExperence;}
    
    /**
     *
     * @return  first name 
     */
    public String getFirstName() 
    {return this.firstname;}
    
    /**
     *
     * @returns  surname
     */
    public String getSurname() 
    {return this.surname;}
    
    /**
     *
     * @return SSN
     */
    public String getSSN()
    {return this.ssn;}
    
    /**
     *
     * @return email
     */
    public String getEmail()
    {return this.email;}
    
    /**
     *
     * @return to Date
     */
    public String getToDate()
    {return this.toDate;}
    
    /**
     *
     * @return from Date
     */
    public String getFromDate()
    {return this.fromDate;}
    
    /**
     *
     * @return Competence
     */
    public String getCompetence()
    {return this.competence;}
    
    /**
     *
     * @return years of Exp
     */
    public String getYearOfExperence()
    {return this.yearOfExperence;}
    
    /**
     *
     * @param loginStatus
     */
    public void setLoginStatus(boolean loginStatus) 
    {this.loginStatus = loginStatus;}
    
    /**
     *
     * @return loginStatus
     */
    public boolean getLoginStatus()
    {return this.loginStatus;}
  
     
    /**
     *
     * @return a boolean when application is created
     */
    public void createApplication(){
      startConversation();
      List<DateList> dl = new LinkedList <DateList>();
      List<CompetenceList> cl = new LinkedList<CompetenceList>();
      
      CompetenceList c  =  new CompetenceList(competence,makeDouble(yearOfExperence));
      DateList d = new DateList(MakeDate(fromDate),MakeDate(toDate));
      dl.add(d);
      cl.add(c);
            
      Application ap = new Application(firstname,surname,ssn,email,dl,cl,makeDouble(yearOfExperence));
      controller.createApplicationFacade(ap);     
 
    }
    
     
    /**
     *
     * @return a Date object from a string
     */
   private Date MakeDate(String date){
    DateFormat  formatter = new SimpleDateFormat("dd-MMM-yy");
        try {
            return (Date)formatter.parse(date);
        } catch (ParseException ex) {
            Logger.getLogger(Applicationbean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
     
    /**
     *
     * @return a Double object from a string
     */
   private Double makeDouble(String yearsOfEx){
         return Double.parseDouble(yearsOfEx);
    }
 
   /**
     *
     * @param List of DateList object 
     * 
     */
    public void setDateList(List<DateList> dl){
     
        this.dateList = dl;
    }
    
      /**
     *
     * @return a List of DateList object
     */
     public List<DateList> getDateList(){
     
        return this.dateList;
    }
     /**
     *
     * @param List of CompetenceList object 
     */
     public void setCompList(List<CompetenceList> cl){
     
        this.comList = cl;
    }
    
     /**
     *
     * @return a List of CompetenceList object
     */
     public List<CompetenceList> getCompList(){
     
        return this.comList;
    }

  
}
